package myJlite.controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jvlite.filter.FilterRequestContext;
import jvlite.mvc.annotaion.Action;
import jvlite.mvc.annotaion.ActionMetod;
import jvlite.mvc.annotaion.Insert;
import jvlite.mvc.annotaion.MethodAuth;
import jvlite.mvc.annotaion.PointCut;
import jvlite.render.IRender;
import jvlite.util.BeanUtil;
import jvlite.util.JLiteJson;
import myJlite.entity.ProfitVO;
import myJlite.entity.StaffVO;
import myJlite.service.IChartsService;
@Action(value = "charts")
@PointCut(advice = "examAdvice", value = "")
public class ChartsCtrl implements IChartsCtrl {

	@Insert(value = "chartsService")
	private IChartsService chartsService;
	@Insert(value = "examRender")
	private IRender render;

	@Override
	@ActionMetod(value="salaryDept")
	@MethodAuth(requirePower = { "user" })
	public void readDeptSlr(HttpServletRequest req, HttpServletResponse res) {
		HttpServletRequest request = FilterRequestContext.get().getRequest();
		HttpServletResponse response = FilterRequestContext.get().getResponse();
		Integer dept_id = Integer.valueOf(request.getParameter("dept_id"));
		dept_id = dept_id == null ? 1 : dept_id;
		List<StaffVO> stLst = null;
		try {
			stLst = this.chartsService.readDeptSlr(dept_id);
//			this.render.renderJSON(response, stLst);
			String stLStJson = JLiteJson.self().toJson(stLst);
			this.render.renderPage(request, response, "/salary.jsp" , stLStJson);
		} catch (SQLException e) {
			e.printStackTrace();
			this.render.renderError(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@ActionMetod(value="profitDept")
	@MethodAuth(requirePower = { "operator" })
	@Override
	public void readProfitByDeptId(HttpServletRequest request,
			HttpServletResponse response) {
		Integer dept_id = Integer.valueOf(request.getParameter("dept_id"));
		dept_id = dept_id == null ? 1 : dept_id;
		try {
			List<ProfitVO> profitByDeptId = chartsService.readProfitByDeptId(dept_id);
			String jsonStr = JLiteJson.self().toJson(profitByDeptId);
			this.render.renderPage(request, response, "/profit.jsp", jsonStr);
		} catch (ServletException | IOException | SQLException e) {
			e.printStackTrace();
		}
	}
	
	@ActionMetod(value="login")
	public void login(HttpServletRequest req, HttpServletResponse res){
		try {
			StaffVO staffVO = (StaffVO)BeanUtil.reqToBean(req, StaffVO.class, true);
			Boolean isLegal = this.chartsService.login(staffVO);
//			if(isLegal){req.getSession().setAttribute("curUser", staffVO);}
			if (isLegal) {
				this.render.renderPage(req, res, "/index.jsp", staffVO.getUnm());
			} else {
				this.render.renderError(req, res);
			}
		} catch (InstantiationException
				| IllegalAccessException | SQLException | ServletException | IOException e) {
			e.printStackTrace();
		}
	}
	
}
